﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using tiDE;

namespace TableFileEditor
{
    public partial class Form1 : Form
    {
        List<OPCode> Opcodes = new List<OPCode>();
        string FileName = "";

        public Form1()
        {
            InitializeComponent();
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.ShowDialog();
            FileName = dialog.FileName;
            BinaryFormatter format = new BinaryFormatter();
            Stream stream = dialog.OpenFile();
            Opcodes = (List<OPCode>)format.Deserialize(stream);
            stream.Close();
            UpdateList();
        }

        private void UpdateList()
        {
            listBox1.Items.Clear();
            foreach (OPCode code in Opcodes)
            {
                listBox1.Items.Add(code.Match);
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1 || dontUpdate)
                return;
            // Match
            Opcodes[listBox1.SelectedIndex].Match = textBox1.Text;
            UpdateList();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1 || dontUpdate)
                return;
            //  Value
            Opcodes[listBox1.SelectedIndex].Value = textBox2.Text;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1 || dontUpdate)
                return;
            // Size
            try
            {
                Opcodes[listBox1.SelectedIndex].Size = byte.Parse(textBox3.Text);
            }
            catch { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BinaryFormatter format = new BinaryFormatter();
            Stream stream = File.Create(FileName);
            format.Serialize(stream, Opcodes);
            stream.Close();
        }

        bool dontUpdate = false;

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
                return;
            dontUpdate = true;
            textBox1.Text = Opcodes[listBox1.SelectedIndex].Match;
            textBox2.Text = Opcodes[listBox1.SelectedIndex].Value;
            textBox3.Text = Opcodes[listBox1.SelectedIndex].Size.ToString();
            dontUpdate = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Move Up
            if (listBox1.SelectedIndex == -1 || listBox1.SelectedIndex == 0)
                return;
            dontUpdate = true;
            OPCode code = Opcodes[listBox1.SelectedIndex];
            Opcodes.RemoveAt(listBox1.SelectedIndex);
            Opcodes.Insert(listBox1.SelectedIndex - 1, code);
            int newIndex = listBox1.SelectedIndex - 1;
            dontUpdate = false;
            UpdateList();
            listBox1.SelectedIndex = newIndex;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // Move Down
            if (listBox1.SelectedIndex == -1 ||
                listBox1.SelectedIndex == listBox1.Items.Count)
                return;
            dontUpdate = true;
            OPCode code = Opcodes[listBox1.SelectedIndex];
            Opcodes.RemoveAt(listBox1.SelectedIndex);
            Opcodes.Insert(listBox1.SelectedIndex + 1, code);
            int newIndex = listBox1.SelectedIndex + 1;
            dontUpdate = false;
            UpdateList();
            listBox1.SelectedIndex = newIndex;
        }
    }
}
